
import 'package:flutter/cupertino.dart';
import 'package:flutter_book/api/search/search_api.dart';
import 'package:flutter_book/constant/style.dart';
import 'package:flutter_book/util/extensions/string_extensions.dart';

class SearchKeyWordsPage extends StatelessWidget {
  final String searchText;

  SearchKeyWordsPage(this.searchText);

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
        future: SearchApi.init().getSearchWord(searchText),
        builder: (BuildContext context, AsyncSnapshot<List<String>> snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            if (snapshot.hasError) {
              snapshot.error.toString().print;
              snapshot.error.toString().dialog;
              return  Container();
            }
            if (snapshot.data==null||snapshot.data!.length<1) {
              return Container();
            }
            return  _getListView(snapshot.data as List<String>);
          }
          return Container();
        }
    );
  }


  Widget _getListView(List<String> dataList){
    return Container(
      child: ListView.builder(
        padding: const EdgeInsets.all(16),
        physics: AlwaysScrollableScrollPhysics(),
        itemCount: dataList.length,
        itemBuilder: (context,index){
          return _getItem(dataList[index]);
        },
      ),
    );
  }

  Widget _getItem(String text){
    return Container(
      padding: const EdgeInsets.all(8),
      child: Text(text,style:AppStyle.textMedium16),
    );
  }
}
